package com.ptszyxx.popose.module.main.mine.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.enums.ActionEnum;
import com.ptszyxx.popose.common.enums.MineFollowTabEnum;
import com.ptszyxx.popose.common.event.UserEditEvent;
import com.ptszyxx.popose.common.event.VipEvent;
import com.ptszyxx.popose.common.oos.YOosUtil;
import com.ptszyxx.popose.common.utils.YActionEventUtil;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ptszyxx.popose.common.utils.YChatUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.RxBus;
import com.ysg.bus.RxSubscriptions;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.mine.MineWalletEntity;
import com.ysg.http.data.entity.mine.SignAddResult;
import com.ysg.http.data.entity.mine.SignResult;
import com.ysg.http.data.entity.mine.UserConfigEntity;
import com.ysg.http.data.entity.mine.VipResult;
import com.ysg.http.data.entity.user.UserResult;
import com.ysg.utils.YImageUtil;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;
import com.ysg.utils.YUserUtil;
import com.ysg.widget.pic.YPicUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class MineWorldVM extends BaseViewModel<CommonRepository> {
    public ObservableField<UserResult> entity =  new ObservableField<>();
    public ObservableField<MineWalletEntity> walletEntity = new ObservableField();
    public VipResult vipResult;

    public MineWorldVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    @Override
    public void requestUploadAction() {
        YActionEventUtil.getInstance().upload(this, model, ActionEnum.mine);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onVipEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onSignEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onAvatarEditEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onAvatarZoomEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onUserDetailCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpHomeWorldDetail(MineWorldVM.this, entity.get().getId()+""));
    public BindingCommand onWalletCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpMineWallet(MineWorldVM.this));
    public BindingCommand onSetCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpMineSet(MineWorldVM.this));
    public BindingCommand onVipCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpMineVip(MineWorldVM.this));
    public BindingCommand onFollowCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpMineFollow(this, MineFollowTabEnum.follow));
    public BindingCommand onFansCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpMineFollow(this, MineFollowTabEnum.fans));
    public BindingCommand onLookCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpMineLook(this));
    public BindingCommand onDynamicCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpMineDynamic(this));
    public BindingCommand onSignCommand = new BindingCommand(()-> requestSignList());
    public BindingCommand onTaskCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpMineTask(this));
    public BindingCommand onRealCommand = new BindingCommand(()-> onReal());

    public BindingCommand onInviteCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpMineInvite(this));
    public BindingCommand onAgreementCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpMineAgreement(this));
    public BindingCommand onShopCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpMineShop(this));
    public BindingCommand onCustomerServiceCommand = new BindingCommand(()-> requestSetup());
    public BindingCommand onAvatarZoomCommand = new BindingCommand(()-> uc.onAvatarZoomEvent.call());


    //TODO 3.异步通知处理=====================
    public Disposable disposableUser, disposableVip;
    @Override
    public void registerRxBus() {
        super.registerRxBus();
        disposableUser = RxBus.getDefault().toObservable(UserEditEvent.class)
                .subscribe(new Consumer<UserEditEvent>() {
                    @Override
                    public void accept(UserEditEvent event){
                        if (event != null){
                            requestUser();
                        }
                    }
                });
        disposableVip = RxBus.getDefault().toObservable(VipEvent.class)
                .subscribe(new Consumer<VipEvent>() {
                    @Override
                    public void accept(VipEvent event){
                        if (event != null){
                            requestVip();
                        }
                    }
                });
    }

    @Override
    public void removeRxBus() {
        super.removeRxBus();
        RxSubscriptions.remove(disposableUser);
        RxSubscriptions.remove(disposableVip);
    }


    //=========================================接口：start===========================================
    /**
     * 用户信息
     */
    public void requestUser(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.user(map), this,
                        new OnSuccessResult<UserResult>() {
                            @Override
                            public void onSuccessResult(BaseResponse<UserResult> response) {
                                entity.set(response.getData());
                                //TODO 设置IM的信息：如头像，昵称等等
                                YChatUtil.getInstance().setInfo(response.getData());
                                //如果是默认头像
                                if (YStringUtil.isAvatarDefault(entity.get().getPic())){
                                    uc.onAvatarEditEvent.call();
                                    YOosUtil.getInstance().getOosToken(model, MineWorldVM.this);
                                }
                            }
                        });
    }

    /**
     * vip列表
     */
    public void requestVip(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.vip(map), this, new OnSuccessResult<VipResult>() {
                    @Override
                    public void onSuccessResult(BaseResponse<VipResult> response) {
                        vipResult = response.getData();
                        uc.onVipEvent.call();
                    }
                });
    }

    /**
     * 签到记录
     */
    public void requestSignList(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.signList(map), this, new OnSuccessResult<SignResult>() {
                    @Override
                    public void onSuccessResult(BaseResponse<SignResult> response) {
                        uc.onSignEvent.setValue(response.getData());
                    }
                });
    }

    /**
     * 签到
     */
    public void requestSignAdd(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.signAdd(map), this, new OnSuccessResult<SignAddResult>() {
                    @Override
                    public void onSuccessResult(BaseResponse<SignAddResult> response) {
                        YToastUtil.showShort(response.getMsg());
                    }
                });
    }
    /**
     * 签到提醒
     */
    public void requestSignTip(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.signTip(map), this, new OnSuccessResult() {
                    @Override
                    public void onSuccessResult(BaseResponse response) {
                    }
                });
    }
    /**
     * 客服
     */
    private void requestSetup() {
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.setup(map), this,
                        new OnSuccessResult<UserConfigEntity>() {
                            @Override
                            public void onSuccessResult(BaseResponse<UserConfigEntity> response) {
                                YActivityUtil.getInstance().jumpWeb(MineWorldVM.this, response.getData().getKefuUrl());
                            }
                        });
    }

    /**
     * 上传oos图片
     */
    public void requestOosUploadPic(String path) {
        YOosUtil.getInstance().upload(path, YSPUtils.getInstance().getUserId(), true, new YOosUtil.OnOosListener() {
            @Override
            public void onSuccess(String url) {
                requestUploadAvatar(url);
            }

            @Override
            public void onSuccess(List<String> list) {
            }

            @Override
            public void onError() {
                YToastUtil.showShort("上传头像失败");
            }
        });
    }

    /**
     * 上传头像
     */
    public void requestUploadAvatar(String url){
        Map<String, String> map = new HashMap<>();
        map.put("pic", url);
        HttpUtils.getInstance()
                .data(model.userEditAvatar(map), this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                                entity.get().setPic(url);
                                YUserUtil.saveAvatar(url);
                            }
                        });
    }

    /**
     * 钱包
     */
    public void requestWallet(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.wallet(map), this, (response) -> walletEntity.set((MineWalletEntity)response.getData()));
    }

    /**
     * 处理实名认证
     */
    private void onReal() {
        if (entity.get() == null){
            return;
        }
        UserResult user = entity.get();
        if(YStringUtil.eq(2, user.getIsReal())){
            YToastUtil.showShort(R.string.mine_real_tip);
        }else   if(YStringUtil.eq(1, user.getIsReal())){
            YToastUtil.showShort(R.string.mine_real_tip_loading);
            return;
        }else{
            YActivityUtil.getInstance().jumpMineReal(this);
        }
    }

}
